package cn.life.business.material.service.impl;

import cn.life.business.material.dao.MaterialDao;
import cn.life.business.material.entity.po.MaterialAuditPo;
import cn.life.business.material.entity.po.MaterialPo;
import cn.life.business.material.entity.po.MaterialRecPo;
import cn.life.business.material.entity.po.MaterialTypePo;
import cn.life.business.material.entity.vo.MaterialRecVo;
import cn.life.business.material.service.MaterialService;
import cn.life.business.user.dao.UserDao;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.entity.vo.PushVo;
import cn.life.common.push.PushService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

/**
 * @Description:
 * @Author 李佳明
 * @Date 2018-07-26 10:17
 */
@Service
@Transactional
public class MaterialServiceImpl implements MaterialService{

    @Autowired
    private MaterialDao materialDao;

    @Autowired
    private UserDao userDao;

    @Override
    public CMessage addMaterialRecord(MaterialRecVo materialRecVo){
        Long recordId = materialDao.addMaterialRecord(materialRecVo);
        if(recordId != null){
            materialRecVo.setRecordId(recordId);

            //审核记录表中增加数据
            //审核记录表中增加数据
            MaterialAuditPo auditPo = new MaterialAuditPo();
            auditPo.setRecordId(materialRecVo.getRecordId());
            auditPo.setOprUserId(materialRecVo.getNextUserId());
            auditPo.setOprUserName(materialRecVo.getNextUserName());
            auditPo.setOprState(0);
            auditPo.setRecState(1);

            if(materialDao.addMaterialDetailRecord(materialRecVo) == 1 && materialDao.addMaterialAuditRecord(auditPo) == 1){
                //1.提料单增加通知技术监管人员审核
                List<Map<String,Object>> mapList = userDao.queryUserList(materialRecVo.getNextUserId().toString());
                try {
                    new PushService().allPush("提料单","您有新的提料单需要审核，请查收!",mapList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return CMessage.fail("推送信息异常");
                }
                return CMessage.addSuccess();
            }
        }
        return CMessage.addFail();
    }

    @Override
    public CMessage updateMaterialRecord(MaterialRecVo materialRecVo) {
        //1.删除提料单明细表信息
        materialDao.deleteMaterialDetailRecord(materialRecVo);
        //2.增加提料单明细表信息
        materialDao.addMaterialDetailRecord(materialRecVo);

        //3.审核记录表中增加数据
        MaterialAuditPo auditPo = new MaterialAuditPo();
        auditPo.setRecordId(materialRecVo.getRecordId());
        auditPo.setOprUserId(materialRecVo.getNextUserId());
        auditPo.setOprUserName(materialRecVo.getNextUserName());
        auditPo.setOprState(0);
        auditPo.setRecState(1);
        materialDao.addMaterialAuditRecord(auditPo);

        //4.修改提料单主表信息
        materialDao.updateMaterialRecord(materialRecVo);
        return CMessage.successByMsg("修改成功");
    }

    @Override
    public CMessage queryRecordList(MyUserInfo myUserInfo,@RequestBody MaterialRecVo materialRecVo) {
        Pagination<MaterialRecVo> result = new Pagination<MaterialRecVo>();
        List<MaterialRecVo> list = new ArrayList<>();
        /*List<MaterialRecVo> auditList = materialDao.queryRecordList(myUserInfo.getUserId(),materialRecVo.getRecordName(),materialRecVo.getProjectId());
        list.addAll(auditList);
        List<MaterialRecVo> oprList = materialDao.queryOprRecordList(myUserInfo.getUserId(),materialRecVo.getRecordName(),materialRecVo.getProjectId());
        list.addAll(oprList);*/

        list = materialDao.queryMaterialRecList(materialRecVo.getRecordName(),materialRecVo.getProjectId(),myUserInfo.getUserId());

        result.setPageIndex(myUserInfo.getPageIndex());
        result.setPageSize(myUserInfo.getPageSize());

       if(list != null && list.size() > 0 && materialRecVo.getStatusStr().equals("my")){
            for(int i=list.size()-1; i>=0; i--){
                if(!list.get(i).getOprUserId().toString().equals(myUserInfo.getUserId().toString())){
                    list.remove(list.get(i));
                }
            }
        }

        if(list != null && list.size() > 0 && materialRecVo.getStatusStr().equals("myAudit")){
            for(int i=list.size()-1; i>=0; i--){
                if(list.get(i).getIsMyAudit() != 1){
                    list.remove(list.get(i));
                }
            }
        }

        result.setTotal(list.size());

        int startNumber = materialRecVo.getPageIndex() * materialRecVo.getPageSize();
        int endNumber = (materialRecVo.getPageIndex() + 1) * materialRecVo.getPageSize() - 1;
        if(startNumber > list.size()){
            result.setRows(new ArrayList<MaterialRecVo>());
        }else if(endNumber <= list.size()){
            result.setRows(list.subList(startNumber,endNumber));
        }else if (endNumber > list.size()){
            endNumber = list.size();
            result.setRows(list.subList(startNumber,endNumber));
        }

        return CMessage.successByData(result);
    }

    @Override
    public CMessage auditMaterialRecord(MaterialRecPo materialRecPo) {
        MaterialAuditPo materialAuditPo = new MaterialAuditPo();
        BeanUtils.copyProperties(materialRecPo,materialAuditPo);
        materialAuditPo.setOprUserId(materialRecPo.getNextUserId());
        materialAuditPo.setOprUserName(materialRecPo.getNextUserName());
        materialAuditPo.setOprState(materialRecPo.getState());

        if(materialAuditPo.getOprState() == 2 || materialAuditPo.getOprState() == 3){
            //审核流程结束情况下
            if(materialAuditPo.getOprState() == 3){
                //不通过情况，将现有审核记录作废
                materialDao.updateMaterialAuditRecord(materialAuditPo.getRecordId());
            }
            //更改主表审核状态信息
            materialDao.auditMaterialRecord(materialRecPo);
            //更改审核表审核状态信息
            materialDao.updateMaterialAuditRecord(materialAuditPo.getRecordId(),materialRecPo.getState());
            List<Map<String,Object>> mapList = userDao.queryUserList(materialRecPo.getOprUserId().toString());
            try {
                String content = "您的提料单已批准，请联系库管提货";
                if(materialAuditPo.getOprState() == 3){
                    content = "您的提料单已驳回，详情请咨询库管";
                }
                new PushService().allPush("提料单",content,mapList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return CMessage.successByMsg("审核成功");
        }else if(materialAuditPo.getOprState() == 1){
            if(materialAuditPo.getOprUserId() == null){
                return CMessage.fail("请选择下一审核人员，再进行。");
            }
            //更改审核表审核状态信息
            materialDao.updateMaterialAuditRecord(materialAuditPo.getRecordId(),materialRecPo.getState());
            //审核流程继续情况下
            materialAuditPo.setOprState(0);
            if(materialDao.addMaterialAuditRecord(materialAuditPo) == 1){
                //1.提料单增加通知技术监管人员审核
                List<Map<String,Object>> mapList = userDao.queryUserList(materialRecPo.getNextUserId().toString());
                try {
                    new PushService().allPush("提料单","您有新的提料单需要审核，请查收!",mapList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return CMessage.successByMsg("审核成功");
            }
        }else{
            return CMessage.fail("请输入正确的审核类型，1.下一步，2.审核通过，3.审核不通过");
        }
        return CMessage.fail("审核失败");
    }

    @Override
    public CMessage addMaterial(MaterialPo materialPo) {
        return materialDao.addMaterial(materialPo) == 1 ? CMessage.addSuccess() : CMessage.addFail();
    }

    @Override
    public CMessage updateMaterial(MaterialPo materialPo) {
        return materialDao.updateMaterial(materialPo) == 1 ? CMessage.modifySuccess() : CMessage.modifyFail();
    }

    @Override
    public CMessage deleteMaterial(MaterialPo materialPo) {
        return materialDao.deleteMaterial(materialPo) == 1 ? CMessage.removeSuccess() : CMessage.removeFail();
    }

    @Override
    public CMessage addMaterialType(MaterialTypePo materialTypePo) {
        String typeCode = materialDao.queryMaxTypeCode(materialTypePo);
        materialTypePo.setTypeCode(typeCode);
        return materialDao.addMaterialType(materialTypePo) == 1 ? CMessage.addSuccess() : CMessage.addFail();
    }

    @Override
    public CMessage updateMaterialType(MaterialTypePo materialTypePo) {
        return materialDao.updateMaterialType(materialTypePo) == 1 ? CMessage.modifySuccess() : CMessage.modifyFail();
    }

    @Override
    public CMessage deleteMaterialType(MaterialTypePo materialTypePo) {
        return materialDao.deleteMaterialType(materialTypePo) == 1 ? CMessage.removeSuccess() : CMessage.removeFail();
    }

    @Override
    public CMessage queryMaterialList(MaterialPo materialPo) {
        Pagination<MaterialPo> list = materialDao.queryMaterialList(materialPo);
        return CMessage.successByData(list);
    }

    @Override
    public CMessage queryMaterialDetail(MaterialPo materialPo) {
        materialPo = materialDao.queryMaterialDetail(materialPo);
        return CMessage.successByData(materialPo);
    }

    @Override
    public CMessage queryMaterialTypeList(MaterialTypePo materialTypePo) {
        List<MaterialTypePo> list = materialDao.queryMaterialTypeList(materialTypePo);
        return CMessage.successByData(list);
    }

    @Override
    public CMessage queryMaterialTypeDetail(MaterialTypePo materialTypePo) {
        materialTypePo = materialDao.queryMaterialTypeDetail(materialTypePo);
        return CMessage.successByData(materialTypePo);
    }

    @Override
    public CMessage queryRecordDetail(MaterialRecVo materialRecVo) {
        return CMessage.successByData(materialDao.queryRecordDetail(materialRecVo));
    }
}
